package com.pillpoppers.pillbox;

import android.app.Fragment;
import android.os.Bundle;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.pillpoppers.pillbox.Schedule;
import com.pillpoppers.pillbox.database.Pill;
import com.pillpoppers.pillbox.database.PillboxDB;
import com.pillpoppers.pillbox.database.pillDB;
import java.util.ArrayList;

public class ScheduleFragment extends Fragment 
{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_schedule, container, false);
		
		updateSchedules(view);
		
		return view;
	}
	
	public void onStart()
	{
		super.onStart();
		
		View view = this.getView();
		updateSchedules(view);
	}
	
	public int calcHours(int hours)
	{
		if(hours >= 12)
		{
			if(hours != 12)
				hours -= 12;
		}
		
		else if(hours == 0)
			hours = 12;
		
		return hours;
	}
	
	public boolean calcTimePeriod(int hours)
	{
		if(hours >= 12)
			return true;
					
		else
			return false;
	}
	
	public boolean checkNumberOfSchedules(int scheduleDayCount)
	{
		if(scheduleDayCount < 6)
			return true;
		
		else 
			return false;
	}
	
	public void updateSchedules(View view)
	{
		PillboxDB db = new PillboxDB(getActivity().getApplicationContext());
		pillDB pDB = new pillDB(getActivity().getApplicationContext());
		
		ArrayList<Schedule> sched = db.getSchedules();
		
		int schedulesOnMonday = 0;
		int schedulesOnTuesday = 0;
		int schedulesOnWednesday = 0;
		int schedulesOnThursday = 0;
		int schedulesOnFriday = 0;
		int schedulesOnSaturday = 0;
		int schedulesOnSunday = 0;
		
		ArrayList<Pill> pills = pDB.getPills();
		
		for(Schedule current: sched)
		{
			if(current != null)
			{
				final int scheduleId = current.getScheduleId();
				ArrayList<Integer> pillIDs = current.getPillIds();
				
				ArrayList<String> pillNames = new ArrayList<String>();
				
				for(Integer curID : pillIDs)
				{
					for(Pill curPill : pills)
					{
						if(curID == curPill.getPillID())
							pillNames.add(curPill.getName());
					}
				}
				
				boolean isPM = false;
				
				int currentDay = current.getDayOfWeek();
				int hours = current.getHour();
				int minutes = current.getMinute();
				
				isPM = calcTimePeriod(hours);
				hours = calcHours(hours);
				
				//current day is Sunday
				if(currentDay == 1 && checkNumberOfSchedules(schedulesOnSunday))
				{
					String textViewID = "sundayTextView" + schedulesOnSunday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView sundayText = (TextView) view.findViewById(resID);
					
					sundayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});
					
					if(minutes < 10)
					{
						sundayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							sundayText.setText(sundayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					else
					{
						sundayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							sundayText.setText(sundayText.getText() + "" + pillNames.get(i) + " ");	
					}
					
					schedulesOnSunday++;
				}
				
				//current day is Monday
				else if(currentDay == 2 && checkNumberOfSchedules(schedulesOnMonday))
				{
					String textViewID = "mondayTextView" + schedulesOnMonday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView mondayText = (TextView) view.findViewById(resID);
					
					mondayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});
					
					if(minutes < 10)
					{
						mondayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							mondayText.setText(mondayText.getText() + "" + pillNames.get(i) + " ");	
					}
					
					else
					{
						mondayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							mondayText.setText(mondayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					schedulesOnMonday++;
				}
				
				
				//current day is Tuesday
				else if(currentDay == 3 && checkNumberOfSchedules(schedulesOnTuesday))
				{
					String textViewID = "tuesdayTextView" + schedulesOnTuesday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView tuesdayText = (TextView) view.findViewById(resID);
					
					tuesdayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});
					
					if(minutes < 10)
					{
						tuesdayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							tuesdayText.setText(tuesdayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					else
					{
						tuesdayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							tuesdayText.setText(tuesdayText.getText() + "" + pillNames.get(i) + " ");	
					}
					
					schedulesOnTuesday++;
				}
			
				//current day is Wednesday
				else if(currentDay == 4 && checkNumberOfSchedules(schedulesOnWednesday))
				{
					String textViewID = "wednesdayTextView" + schedulesOnWednesday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView wednesdayText = (TextView) view.findViewById(resID);
					
					wednesdayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});
					
					if(minutes < 10)
					{
						wednesdayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							wednesdayText.setText(wednesdayText.getText() + "" + pillNames.get(i) + " ");	
					}
					
					else
					{
						wednesdayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							wednesdayText.setText(wednesdayText.getText() + "" + pillNames.get(i) + " ");	
					}
					
					schedulesOnWednesday++;
				}
			
				//current day is Thursday
				else if(currentDay == 5 && checkNumberOfSchedules(schedulesOnThursday))
				{
					String textViewID = "thursdayTextView" + schedulesOnThursday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView thursdayText = (TextView) view.findViewById(resID);
					
					thursdayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});
					
					if(minutes < 10)
					{
						thursdayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							thursdayText.setText(thursdayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					else
					{
						thursdayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							thursdayText.setText(thursdayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					schedulesOnThursday++;
				}
			
				//current day is Friday
				else if(currentDay == 6 && checkNumberOfSchedules(schedulesOnFriday))
				{
					String textViewID = "fridayTextView" + schedulesOnFriday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView fridayText = (TextView) view.findViewById(resID);
					
					fridayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});
					
					if(minutes < 10)
					{
						fridayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							fridayText.setText(fridayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					else
					{
						fridayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							fridayText.setText(fridayText.getText() + "" + pillNames.get(i) + " ");				
					}
					
					schedulesOnFriday++;
				}
			
				//current day is Saturday
				else if(currentDay == 7 && checkNumberOfSchedules(schedulesOnSaturday))
				{
					String textViewID = "saturdayTextView" + schedulesOnSaturday;
				    int resID = getResources().getIdentifier(textViewID, "id", "com.pillpoppers.pillbox");
					TextView saturdayText = (TextView) view.findViewById(resID);
					
					saturdayText.setOnClickListener(new View.OnClickListener() {

						  @Override
						  public void onClick(View v) 
						  {
							  Intent intent = new Intent(getActivity(), AlarmSettingActivity.class);
							  intent.putExtra("scheduleId", scheduleId);
							  startActivity(intent);
						  }

						});

					if(minutes < 10)
					{
						saturdayText.setText(hours + ":" + "0" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						for(int i = 0; i < pillNames.size(); i++)
							saturdayText.setText(saturdayText.getText() + "" + pillNames.get(i) + " ");
					}
					
					else
					{
						saturdayText.setText(hours + ":" + minutes + " " + (isPM? "PM" : "AM") + " - ");
						
						for(int i = 0; i < pillNames.size(); i++)
							saturdayText.setText(saturdayText.getText() + "" + pillNames.get(i) + " ");					
					}
					
					schedulesOnSaturday++;
				}
			}
			
		}
	}

}
